﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWA.VAT.DbAccess.SQL;

namespace AWA.VAT.GuestRelations
{
    public class WinnersPageBuilder
    {
        SQLLookup _lookup;

        public WinnersPageBuilder(SQLLookup lookup)
        {
            this._lookup = lookup;
        }

        public string BuildAwardsPage(int year)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine(String.Format("<h1>The Video Art Track at Anime Weekend Atlanta {0}</h1>", year.ToString()));

            Dictionary<Contest, List<Entry>> contestWinners = _lookup.GetWinners(year);
            List<Contest> contests = new List<Contest>();
            foreach (Contest key in contestWinners.Keys)
            {
                contests.Add(key);
            }
            contests.Sort(new ContestComparer());

            foreach (Contest contest in contests)
            {
                string shortName = contest.ContestName.Replace(" ", String.Empty);
                builder.AppendLine(String.Format("<a href=\"#{0}\">{1}</a> results<br />", shortName, contest));
            }

            foreach (Contest contest in contests)
            {
                string shortName = contest.ContestName.Replace(" ", String.Empty);
                builder.AppendLine(String.Format("<h1><a href=\"#\" name=\"{0}\"></a>AWA {1} {2} Results</h1>", shortName, year, contest));
                
                foreach (Entry entry in contestWinners[contest])
                {
                    builder.AppendLine(BuildEntryLine(entry));
                    builder.AppendLine("<br>");
                }
            }
            return builder.ToString();
        }

        private string BuildEntryLine(Entry entry)
        {
            string awards = String.Empty;
            foreach (Award award in entry.Awards)
                awards += award.AwardName + ", ";
            awards = awards.Substring(0, awards.Length - 2);
            awards = String.Format("<b>{0}</b><br>", awards);
            string songArtist = String.Empty;
            if (entry.AMV.Songs.Count > 3)
                songArtist = String.Format("{0} Various Songs", entry.AMV.Songs.Count);
            else
            {
                foreach (Song song in entry.AMV.Songs)
                    songArtist += String.Format("'{0}' by {1}, ", song.SongName, song.Artist.ArtistName);
                songArtist = songArtist.Substring(0, songArtist.Length - 2);
            }

            string sources = String.Empty;
            if (entry.AMV.Sources.Count > 3)
                sources = String.Format("{0} Various Sources", entry.AMV.Sources.Count);
            else
            {
                foreach (Source source in entry.AMV.Sources)
                    sources += source.SourceName + ", ";
                sources = sources.Substring(0, songArtist.Length - 2);
            }
            string entryLine = String.Format("{0}\n{1} - {2} - {3} - {4}",
                awards, entry.AMV.Editor.CreatorName, entry.AMV.AMVTitle, songArtist, sources);
            string locations = String.Empty;
            foreach (AMVs_Locations location in entry.AMV.AMVs_Locations)
            {
                if (location.IsPublic)
                    locations += String.Format(" - <a href=\"{0}\">Link</a>", location.Location);
            }
            entryLine += locations + "<br>";
            return entryLine;
        }
    }
}
